home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / biblio / bibtex / utils / bibextract / citefind.awk < prev    next >
Text File  |  1992-10-29  |  4KB  |  121 lines

  1. ### ====================================================================
  2. ###  @Awk-file{
  3. ###     author          = "Nelson H. F. Beebe",
  4. ###     version         = "1.02",
  5. ###     date            = "30 October 1992",
  6. ###     time            = "19:41:07 MST",
  7. ###     filename        = "citefind.awk",
  8. ###     address         = "Center for Scientific Computing
  9. ###                        Department of Mathematics
  10. ###                        University of Utah
  11. ###                        Salt Lake City, UT 84112
  12. ###                        USA",
  13. ###     telephone       = "+1 801 581 5254",
  14. ###     FAX             = "+1 801 581 4148",
  15. ###     checksum        = "40982 120 464 4322",
  16. ###     email           = "beebe@math.utah.edu (Internet)",
  17. ###     codetable       = "ISO/ASCII",
  18. ###     keywords        = "BibTeX, bibliography",
  19. ###     supported       = "yes",
  20. ###     abstract        = "This program extracts bibliography entries
  21. ###                        from one or more bibliography files for
  22. ###                        specified citation tags ",
  23. ###     docstring       = "*********************************************
  24. ###                        This code is hereby placed in the PUBLIC
  25. ###                        DOMAIN and may be redistributed without any
  26. ###                        restrictions.
  27. ###                        *********************************************
  28. ###
  29. ###                        Read a list of cite tags from the file
  30. ###                        given by the first argument, then process
  31. ###                        each of the remaining file arguments as
  32. ###                        BibTeX bib files, looking up the
  33. ###                        bibentries, and output them to stdout.
  34. ###                        @preamble{} and @string{} entries are
  35. ###                        automatically output as well.
  36. ###
  37. ###                        Usage:
  38. ###                             nawk -f citefind.awk foo.tags bibfile(s) \
  39. ###                                 >newbibfile
  40. ###
  41. ###                        To be recognized, bib entries must look like
  42. ###
  43. ###                        @keyword{tag,
  44. ###                        ...
  45. ###                        }
  46. ###
  47. ###                        where the start @ appears in column 1, and
  48. ###                        the complete entry has balanced braces.
  49. ###
  50. ###                        This program should normally be run via
  51. ###                        the separate citefind shell script.
  52. ###
  53. ###                        The checksum field above contains a CRC-16
  54. ###                        checksum as the first value, followed by the
  55. ###                        equivalent of the standard UNIX wc (word
  56. ###                        count) utility output of lines, words, and
  57. ###                        characters.  This is produced by Robert
  58. ###                        Solovay's checksum utility.",
  59. ###  }
  60. ### ====================================================================
  61.  
  62. ### Edit history (reverse chronological order):
  63. ### [21-Oct-1992]       1.01    Update for public distribution
  64. ### [12-Apr-1989]       1.00    Original version
  65.  
  66. BEGIN {
  67.     while (FILENAME == ARGV[1]) # process stdin only to get tags
  68.     {
  69.         if (getline <= 0)
  70.             break;
  71.         tagnames[$0] = $0;
  72.     }
  73. }
  74.  
  75. # @string and @preamble -- collect up to paired closing brace
  76.  
  77. /^@([Pp][Rr][Ee][Aa][Mm][Bb][Ll][Ee]|[sS][tT][rR][iI][nN][gG]){/        {
  78.     printbraceditem();
  79.     print "";
  80. }
  81.  
  82. # "@keyword{tag," -- collect up to line starting with right brace
  83. /^@[a-zA-Z0-9]*{/       {
  84.         tag = substr($0,index($0,"{")+1);
  85.         tag = substr(tag,1,index(tag,",")-1);
  86.         if (tagnames[tag] != "")
  87.         {
  88.             printbraceditem();
  89.             print "";
  90.         }
  91.     }
  92.  
  93. function bracecount(s, k,n)
  94. {
  95.     n = 0;
  96.     for (k = 1; k <= length(s); ++k)
  97.     {
  98.         if (substr(s,k,1) == "{")
  99.             n++;
  100.         else if (substr(s,k,1) == "}")
  101.             n--;
  102.     }
  103.     return (n);
  104. }
  105.  
  106. # Starting with the current contents of $0, print lines until we
  107. # reach a zero brace count.
  108. function printbraceditem(count)
  109. {
  110.     count = bracecount($0);
  111.     print $0;
  112.     while (count != 0)
  113.     {
  114.         if (getline <= 0)
  115.             break;
  116.         print $0;
  117.         count += bracecount($0);
  118.     }
  119. }
  120. ########################################################################
  121.